local function TIPS(content)
    TIP("野火警告", "red", content, "chat")
end
local KEY = true
local cooldown = false
AddPrefabPostInit("smoke_plant", function(inst)
    if not KEY then return end
    inst:DoTaskInTime(0.1, function(inst)
        if inst.Transform and GLOBAL.ThePlayer and not cooldown then
            local pos = inst:GetPosition()
            local picker = GLOBAL.ThePlayer.components.playeractionpicker
            if pos and picker then
                local ents = TheSim:FindEntities(pos.x, 0, pos.z, 0.0001, nil, {'FX','DECOR','INLIMBO','NOCLICK', 'player'})
                local smolder
                for _, ent in pairs(ents)do
                    if ent:HasTag("smolder") then
                        smolder = ent
                        break
                    end
                end
                local ssrs = TheSim:FindEntities(pos.x, 0, pos.z, TUNING.FIRE_DETECTOR_RANGE or 15, nil, {'FX','DECOR','INLIMBO','NOCLICK', 'player', 'fueldepleted'})
                for _, ssr in pairs(ssrs)do
                    if ssr.prefab == "firesuppressor" and not c_anim("idle_off", ssr) then
                        smolder = nil
                    end
                end

                if smolder and smolder.name then
                    local state = GLOBAL.TheWorld and GLOBAL.TheWorld.state
                    if state then
                        local threshold = TUNING.WILDFIRE_THRESHOLD or 80
                        if state.issummer and state.isday and type(state.temperature) == "number" and state.temperature >= threshold then
                            TIPS(smolder.name.." 发生自燃！")
                        else
                            if KEY ~= "HIDE" then
                                TIPS(smolder.name.." 被引燃！") 
                            end
                        end
                        cooldown = GLOBAL.ThePlayer:DoTaskInTime(0.5, function()
                            cooldown = false
                        end)
                    end
                end
            end
        end
    end)
end)

-- 仅提示自燃
-- 自燃、引燃都提示
-- 关闭

local Data = "MOD_TIP_WILDFIRES"
local function switchKEY()
    local content = ""
    if KEY == true then
        KEY = "HIDE"
        content = "仅提示自燃"
    elseif KEY == "HIDE" then
        KEY = false
        content = "关闭"
    else
        KEY = true
        content = "自燃引燃都提示"
    end


    TIP("野火警告", "blue", content)
    SaveModData(Data, KEY)
end

local function OnWorldPostInit(inst)
    inst:ListenForEvent("playeractivated", function ()
        KEY = LoadModData(Data)
        if type(KEY) == "nil" then
            KEY = true
        end
    end)
end
AddPrefabPostInit("world", OnWorldPostInit)

if GetModConfigData("sw_wildfires") == "biubiu" then
DEAR_BTNS:AddDearBtn(GLOBAL.GetInventoryItemAtlas("firestaff_flamelash.tex"), "firestaff_flamelash.tex", "野火警告", "燃起来了！", false, switchKEY)
end
AddBindBtn("sw_wildfires", switchKEY)